package sharping.appilog.fragment;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;

import sharping.appilog.DetailCoupon;
import sharping.appilog.R;
import sharping.appilog.app;
import sharping.appilog.Adapter.RHaveCouponAdapter;
import sharping.appilog.DataTree.CouponData;
import sharping.appilog.DataTree.HaveCouponData;
import sharping.appilog.ListItem.HaveCouponItem;
import sharping.appilog.Logic.DownLoadData;
import sharping.appilog.Logic.ScreenSize;
import sharping.appilog.Logic.DownLoadData.OnLoadComplete;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout.LayoutParams;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class Fragment_MycouponLists extends BaseTabListFragment {

	public static DownLoadData downloadCouon, downloadCouon_free;

	ArrayList<HaveCouponData> data, free, premium;
	RHaveCouponAdapter adapter;
	boolean tabposition = true;
	LayoutInflater inflater;
	
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		HashMap<String, String> param = new HashMap<String, String>();
		param.put("UserId", app.UserId);
		downloadCouon = new DownLoadData(HaveCouponData.GET_URL_PRIMIUM, param);
		downloadCouon_free = new DownLoadData(HaveCouponData.GET_URL_FREE, param);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return super.onCreateView(inflater, container, savedInstanceState);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		inflater = getActivity().getLayoutInflater();

		getTab1().setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				tabposition = isChecked;
				setListShown(false);
				if (isChecked) {
					downloadCouon.commit(app.mQueue);
				} else {
					downloadCouon_free.commit(app.mQueue);
				}
			}
		});

		downloadCouon.setOnLoadComplete(new OnLoadComplete() {

			@Override
			public void onLoadComplete(String result, int type) {
				Gson gson = new Gson();
				Type couponType = new TypeToken<ArrayList<HaveCouponData>>() {
				}.getType();
				premium = gson.fromJson(result, couponType);
				
				if (premium.size() == 0) {
					setListEmpty();
					return;
				}
				
				setListAdapter(new RHaveCouponAdapter(getActivity(), premium, inflater));
			}
		});
		
		downloadCouon_free.setOnLoadComplete(new OnLoadComplete() {
			
			@Override
			public void onLoadComplete(String result, int type) {
				Gson gson = new Gson();
				Type couponType = new TypeToken<ArrayList<HaveCouponData>>() {
				}.getType();
				free = gson.fromJson(result, couponType);
				
				if (free.size() == 0) {
					setListEmpty();
					return;
				}
				
				setListAdapter(new RHaveCouponAdapter(getActivity(), free, inflater));
			}
		});
		
		getListView().setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent intent = new Intent(getActivity(), DetailCoupon.class);
				int realposition = arg2 - getListView().getRefreshableView().getHeaderViewsCount();
				if (tabposition) {
					intent.putExtra("CouponData", premium.get(realposition));

				} else {
					intent.putExtra("CouponData", free.get(realposition));
				}
				
				startActivity(intent);
			}
		});

		downloadCouon.commit(app.mQueue);
	}
	
}
